Skip to content

Fix pickling auto_exc exceptions with kw_only fields - #1573

Closed
lin-hongkuan wants to merge 1 commit into
python-attrs:mainfrom
lin-hongkuan:codex/exception-kw-only-reduce
Closed

Fix pickling auto_exc exceptions with kw_only fields#1573
lin-hongkuan wants to merge 1 commit into
python-attrs:mainfrom
lin-hongkuan:codex/exception-kw-only-reduce

Conversation

@lin-hongkuan

Copy link
Copy Markdown

Summary

Fixes #734.

This adds an attrs-generated __reduce__ for auto_exc=True exception classes that have init=True keyword-only fields. The generated reducer reconstructs the exception without calling __init__, then restores the normal pickle state, so required keyword-only fields work across plain, frozen, slotted, and frozen-slotted exception classes.

The generation is intentionally narrow: it only applies to attrs-generated exception __init__ methods with keyword-only init fields, leaves positional-only exceptions on BaseException.__reduce__, and does not override custom __reduce__ implementations, including inherited ones.

Tests

  • PYTHONPATH=src python -m pytest -q
  • PYTHONPATH=src python -m ruff check src/attr/_make.py tests/test_functional.py
  • PYTHONPATH=src python -m ruff format --check src/attr/_make.py tests/test_functional.py
  • git diff --check

@hynek

hynek commented Jul 22, 2026

Copy link
Copy Markdown
Member

Thank you for trying to contribute.

Given the current volume of LLM-assisted contributions, we cannot spend maintainer time reviewing pull requests that do not follow the project’s contribution process.

Before submitting further contributions, please read and follow this project’s CONTRIBUTING.md and AI_POLICY.md.

Thank you for your understanding!

@hynek hynek closed this Jul 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pickling of Exceptions and kw_only

2 participants